n, m = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]


def dfs(i, j):
    if i < 0 or i >= n: return
    if j < 0 or j >= m: return
    if board[i][j] != 1:
        return
    board[i][j] = 0
    dfs(i + 1, j)
    dfs(i - 1, j)
    dfs(i, j + 1)
    dfs(i, j - 1)


cnt = 0
for x in range(n):
    for y in range(m):
        if board[x][y] == 1:
            cnt += 1
            dfs(x, y)
print(cnt)
